-
Notifications
You must be signed in to change notification settings - Fork 4k
[python-package] enforce keyword-only args in more internal functions #7111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
StrikerRUS
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just one minor question below
python-package/lightgbm/callback.py
Outdated
| self.first_metric = "" | ||
|
|
||
| def _gt_delta(self, curr_score: float, best_score: float, delta: float) -> bool: | ||
| def _gt_delta(self, curr_score: float, best_score: float, *, delta: float) -> bool: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious, why not after self?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking that a binary "op" type of function should always begin with 2 positional arguments, just something I'm used to from other languages.
But I see this is only called by lightgbm's own code (not passed through anything from functools or something) and is called like any other normal function:
LightGBM/python-package/lightgbm/callback.py
Line 416 in 33e93d6
| if first_time_updating_best_score_list or self.cmp_op[i](metric_value, self.best_score[i]): |
Seeing that, I think keyword arguments there would improve clarity. I'll make that change (and for _lt_delta).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 2e48184
I had to add a # type: ignore[call-arg] comment. I think mypy is confused by the use of partial() here:
LightGBM/python-package/lightgbm/callback.py
Line 386 in 33e93d6
| self.cmp_op.append(partial(self._gt_delta, delta=delta)) |
python-package/lightgbm/callback.py:416: error: Unexpected keyword argument "curr_score" [call-arg]
python-package/lightgbm/callback.py:416: error: Unexpected keyword argument "best_score" [call-arg]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making calls of these functions even more clear.
borchero
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
Contributes to #3756
Enforces keyword-only arguments in more internal functions, to make the Python codebase a bit stricter and easier to follow.